package com.qjz.util;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.DOMOutputter;

import com.qjz.entity.CrossTo;

/**
 * 数据库查询结果完成行转列效果
 * @author ding_donghong
 */
public class CommonUtil {

	/**
	 * 补充交叉表数据，空数据填充0
	 * 
	 * @param headerList
	 *            表格头列表
	 * @param dataList
	 *            现有数据列表
	 * @return List<CrossTo>
	 */
	public static List<CrossTo> fillCrossReportDate(List<Object> headerList,
			List<CrossTo> dataList) {

		List<List<CrossTo>> rs = new ArrayList<List<CrossTo>>();
		List<List<CrossTo>> rsTemp = new ArrayList<List<CrossTo>>();

		//构建二维数组
		for (int i = 0; i < dataList.size(); i++) {
			if (i==0 || !dataList.get(i).getRow().equals(dataList.get(i-1).getRow())) {
				List<CrossTo> list = new ArrayList<CrossTo>();
				list.add(dataList.get(i));
				rs.add(list);
			} else {
				rs.get(rs.size() - 1).add(dataList.get(i));
			}
		}

		for (int j = 0; j < rs.size(); j++) {
			List<CrossTo> list = rs.get(j);
			List<CrossTo> lTemp = new ArrayList<CrossTo>();

			int i = 0;
			for (Object crossHeader : headerList) {
//				String row = ((CrossTo) crossHeader).getRow();
				String col = ((CrossTo) crossHeader).getCol();
				if (i < list.size()
						&& (col.equals(list.get(i).getCol()))) {
					lTemp.add(list.get(i));
					i++;
				} else {
					CrossTo fill = new CrossTo();
					fill.setCol(((CrossTo)crossHeader).getCol());
					fill.setRow(list.get(0).getRow());
					fill.setValue(0);
					lTemp.add(fill);
				}
			}
			rsTemp.add(lTemp);
		}
		List<CrossTo> result = new ArrayList<CrossTo>();
		for (List<CrossTo> listTemp : rsTemp) {
			result.addAll(listTemp);
		}
		return result;
	}

	/**
	 * 解析数据源为XML
	 * @param list
	 * @return
	 * @throws JDOMException
	 */
	public static org.w3c.dom.Document createCrossXml(List<CrossTo> list)
			throws JDOMException {
		Document doc = new Document();
		Element root = new Element("root");
		doc.setRootElement(root);
		
		if (null != list) {
			Element rowNote = null;
			for (int i = 0; i < list.size(); i++) {
				// 不是同一行
				if (0 == i
						|| !list.get(i).getRow().equals(
								list.get(i - 1).getRow())) {

					rowNote = new Element("rowNote");

					Element rowName = new Element("rowName");
					rowName.setText(list.get(i).getRow());

					Element checkCount = new Element("checkCount"+list.get(i).getCol());
					if (null == list.get(i).getValue()) {
						checkCount.setText("0");
					} else {
						checkCount.setText(list.get(i).getValue().toString());
					}
					rowNote.addContent(rowName);
					rowNote.addContent(checkCount);
					root.addContent(rowNote);
				} else {
					Element checkCount = new Element("checkCount"
							+ list.get(i).getCol());
					if (null == list.get(i).getValue()) {
						checkCount.setText("0");
					} else {
						checkCount.setText(list.get(i).getValue().toString());
					}
					rowNote.addContent(checkCount);
				}
			}
		}
		DOMOutputter outputter = new DOMOutputter();
		return outputter.output(doc);
	}
}
